package com.spzx.common.security.config;

import java.util.concurrent.*;

public class ThreadPoolUtil {
    private ThreadPoolUtil(){}
    private static Executor threadPoolExecutor;
    static {
        //获取服务器核心数
        int processors = Runtime.getRuntime().availableProcessors();
        threadPoolExecutor = new ThreadPoolExecutor(processors*3, 400, 60,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(500), Executors.defaultThreadFactory(),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        //尝试使用线程池继续执行任务
                        try {
                            Thread.sleep(100);
                            executor.execute(r);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
    }
    public static Executor threadPoolExecutor(){
        return threadPoolExecutor;
    }
}